package org.jings.simplemanager.activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.jings.simplemanager.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by JOY on 2016/11/20.
 */

public class GridActivity extends Activity implements AdapterView.OnItemClickListener{

    private GridView gridView;

//    private View keyBoard;

    private View inputBoard;

    private View createLine;

    private View editBtn;

    private TextView showBar;

    private boolean isChoose = false;

    private Integer nowPosition = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.grid_view_layout);

        gridView = (GridView) findViewById(R.id.grid_view);
        SimpleAdapter adapter = new SimpleAdapter(this, this.getData(), R.layout.grid_item, new String[]{"text"}, new int[]{R.id.edit_text});
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(this);

//        keyBoard = findViewById(R.id.key_board);
//        keyBoard.setVisibility(View.INVISIBLE);

        inputBoard = findViewById(R.id.input_board);
        inputBoard.setVisibility(View.INVISIBLE);

        createLine = findViewById(R.id.create_line);
        createLine.setVisibility(View.VISIBLE);

        editBtn = findViewById(R.id.edit_btn);
        editBtn.setVisibility(View.INVISIBLE);

        showBar = (TextView) findViewById(R.id.show_bar);

    }

    private List<Map<String,Object>> getData(){
        List<Map<String, Object>> data = new ArrayList<>();

        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 5; j++) {
                Map<String, Object> map = new HashMap<>();
                if (i < 5) {
                    map.put("text", "(" + (i + 1) + " , " + (j + 1) + ")");
                }
                data.add(map);
            }
        }

        return data;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 选中新格子
        if (!isChoose) {
            nowPosition = position;
            isChoose = true;
            this.showEditBtn();

            TextView text = (TextView) view.findViewById(R.id.edit_text);
            text.setBackgroundColor(Color.CYAN);

            return;
        }

        // 取消选中
        if (nowPosition == position) {
            isChoose = false;
            this.showCreateLine();

            TextView text = (TextView) view.findViewById(R.id.edit_text);
            text.setBackgroundColor(Color.WHITE);

            return;
        }

        // 更换选中
        // 取消之前被选中的格子
        View oldView = gridView.getChildAt(nowPosition);
        TextView oldTextView = (TextView) oldView.findViewById(R.id.edit_text);
        oldTextView.setBackgroundColor(Color.WHITE);

        // 选中新格子
        TextView text = (TextView) view.findViewById(R.id.edit_text);
        text.setBackgroundColor(Color.CYAN);

        showBar.setText(text.getText());

        nowPosition = position;

    }



    private void showEditBtn(){
        editBtn.setVisibility(View.VISIBLE);
        inputBoard.setVisibility(View.INVISIBLE);
        createLine.setVisibility(View.INVISIBLE);
    }

    private void showCreateLine(){
        editBtn.setVisibility(View.INVISIBLE);
        inputBoard.setVisibility(View.INVISIBLE);
        createLine.setVisibility(View.VISIBLE);
    }



    public void onEdit(View view){
        if (View.VISIBLE == inputBoard.getVisibility()) {
            inputBoard.setVisibility(View.INVISIBLE);
        }else{
            String str = "";
            if (nowPosition != null) {
                View itView = gridView.getChildAt(nowPosition);
                TextView itTextView = (TextView) itView.findViewById(R.id.edit_text);
                str = itTextView.getText().toString();
            }

            showBar.setText(str);

            inputBoard.setVisibility(View.VISIBLE);
        }
    }

    public void keyOnClick(View view){
        switch (view.getId()) {
            case R.id.key_0:
            case R.id.key_1:
            case R.id.key_2:
            case R.id.key_3:
            case R.id.key_4:
            case R.id.key_5:
            case R.id.key_6:
            case R.id.key_7:
            case R.id.key_8:
            case R.id.key_9:
            case R.id.key_point:
            case R.id.key_plus:
            case R.id.key_subtract:
            case R.id.key_multiply:
            case R.id.key_divide:

                showBar.append(((TextView)view).getText());
                break;
            case R.id.key_back:
                int len = showBar.getText().length();
                if (len > 0) {
                    showBar.setText(showBar.getText().toString().substring(0, len - 1));
                }
                break;

            case R.id.key_sure:
                View itView = gridView.getChildAt(nowPosition);
                TextView itTextView = (TextView) itView.findViewById(R.id.edit_text);
                itTextView.setText(showBar.getText());
                break;
        }

    }
}
